#!/bin/env python
# -*- coding:utf-8 -*-

import ConfigParser
import os
import re
import sys
import traceback

import pymysql

from downloader.downloader import download, SimpleFileManager


def get_pic(aid):
    print "获取列表[%s]" % aid
    albums = "http://m.wnacg.net/photos-gallery-aid-%d.html" % aid
    mgr = SimpleFileManager()
    download(albums, file_manager=mgr, timeout_second=60)
    urls = [albums]
    for url in re.compile('(http://[^"]+)\\\\').findall(mgr.get_text(albums)):
        if url.endswith('shoucang.jpg'):
            continue
        url = url.replace("/m/", "/data/")
        # url=url.replace("/img.wnacg.us/", "/img.wnacg.net/")
        urls.append(url)
    # 开始下载
    print "开始获取[%s]" % len(urls)
    fails = download(urls, limit='500k', threads=5, file_manager=mgr, timeout_second=60)
    if fails == 0:
        cur.execute('''\
UPDATE %s SET state = 1 WHERE id = %s
''' % (dbtable, aid))
        conn.commit()
    else:
        print "出现错误[%s]" % fails


if __name__ == '__main__':
    db_default = {
        "dbhost": "localhost",
        "dbport": 3306,
        "dbuser": "root",
        "dbpasswd": "root",
        "dbname": "spider",
        "dbtable": "wnacg",
    }
    cf = ConfigParser.ConfigParser()
    conf_file = 'conf.props'
    if not os.path.exists(conf_file):
        cf.add_section("db")
        for key, value in db_default.items():
            cf.set("db", key, value)
        with open(conf_file, mode='w') as out:
            cf.write(out)
    else:
        cf.read(conf_file)
    dbname = cf.get("db", "dbname")
    dbtable = cf.get("db", "dbtable")
    dbhost = cf.get("db", "dbhost")
    dbuser = cf.get("db", "dbuser")
    dbpasswd = cf.get("db", "dbpasswd")

    conn = pymysql.connect(host=dbhost, user=dbuser, passwd=dbpasswd, db=dbname, charset='utf8')
    cur = conn.cursor()
    start = 0
    while True:
        try:
            if cur.execute('''\
SELECT id FROM %s WHERE `state`=0 AND `id` > %d limit 1
''' % (dbtable, start)) == 0:
                # 数据ok了
                break
            data = next(cur.__iter__())
            start = data[0]
            get_pic(data[0])
        except Exception as e:
            sys.stderr.write("[ERROR]%s\n" % e.message)
            traceback.print_exc()
